package com.xm.servlet.background.commodity;

import com.xm.entity.CommoditySpecification;
import com.xm.service.background.CommoditySpecificationService;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 新增规格
 * @author 康文程
 * @date 2024-1-17
 */
@WebServlet("/background/insert/commodity/specification")
public class InsertCommoditySpecificationServlet extends HttpServlet {
    private final CommoditySpecificationService service = new CommoditySpecificationService();
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commodityId = req.getParameter("commodityId");
        Integer specificationsPrice = req.getParameter("specificationsPrice").isEmpty()
                ? 0 : Integer.parseInt(req.getParameter("specificationsPrice"));
        String specificationsTypeId = req.getParameter("specificationsTypeId");
        String specificationsInput = req.getParameter("specificationsInput");
        CommoditySpecification commoditySpecification = new CommoditySpecification();
        commoditySpecification.setCommodityId(Integer.parseInt(commodityId));
        commoditySpecification.setSpecificationTypeId(Integer.parseInt(specificationsTypeId));
        commoditySpecification.setSpecificationName(specificationsInput);
        commoditySpecification.setAddPrice(new BigDecimal(specificationsPrice));
        commoditySpecification.setIsDefault(0);
        resp.getWriter().print(service.insertCommoditySpecification(commoditySpecification));
    }
}
